using System;
using System.IO;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Xml;
using DbDiff.Common.Helper;

namespace DbDiffUtils
{
	public class OptionInfo : DbDiffUtilsBase
	{
		private XmlNode _configNode;

		private Label label1;
		private TextBox txtKey;
		private Label label2;
		private TextBox txtValue;
		private Label label3;
		private Label label4;
		private Label label5;
		private Label label6;
		private Label label7;
		private Label label8;
		private Label label9;
		private TextBox txtRowName;
		private TextBox txtErrorMessage;
		private TextBox txtLabel;
		private CheckBox chkVisible;
		private CheckBox chkReload;
		private CheckBox chkRecompare;
		private ComboBox cmbControlledBy;
		private ComboBox cmbUIType;
		private CheckedListBox chlTypes;
		private System.Windows.Forms.ComboBox cmbUIControlType;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label lblRowNumber;
		private System.Windows.Forms.TextBox txtRowNumber;
		private TextBox txtTooltip;

		public OptionInfo()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			_configNode = null;
		}

		public OptionInfo(XmlNode configNode, StringCollection optionList)
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			SetData(configNode, optionList);
		}

		public void SetData(XmlNode configNode, StringCollection optionList) 
		{
			_configNode = configNode;
			UpdateView(optionList);
		}

		#region Designer generated code
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtKey = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtValue = new System.Windows.Forms.TextBox();
			this.txtRowName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtErrorMessage = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.txtLabel = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.txtTooltip = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.chkVisible = new System.Windows.Forms.CheckBox();
			this.label8 = new System.Windows.Forms.Label();
			this.chkReload = new System.Windows.Forms.CheckBox();
			this.chkRecompare = new System.Windows.Forms.CheckBox();
			this.label9 = new System.Windows.Forms.Label();
			this.cmbControlledBy = new System.Windows.Forms.ComboBox();
			this.cmbUIType = new System.Windows.Forms.ComboBox();
			this.chlTypes = new System.Windows.Forms.CheckedListBox();
			this.cmbUIControlType = new System.Windows.Forms.ComboBox();
			this.label10 = new System.Windows.Forms.Label();
			this.lblRowNumber = new System.Windows.Forms.Label();
			this.txtRowNumber = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Name";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtKey
			// 
			this.txtKey.Location = new System.Drawing.Point(88, 8);
			this.txtKey.Name = "txtKey";
			this.txtKey.Size = new System.Drawing.Size(248, 20);
			this.txtKey.TabIndex = 1;
			this.txtKey.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Value";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtValue
			// 
			this.txtValue.Location = new System.Drawing.Point(88, 32);
			this.txtValue.Name = "txtValue";
			this.txtValue.Size = new System.Drawing.Size(248, 20);
			this.txtValue.TabIndex = 3;
			this.txtValue.Text = "";
			// 
			// txtRowName
			// 
			this.txtRowName.Location = new System.Drawing.Point(88, 56);
			this.txtRowName.Name = "txtRowName";
			this.txtRowName.Size = new System.Drawing.Size(248, 20);
			this.txtRowName.TabIndex = 5;
			this.txtRowName.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(200, 23);
			this.label3.TabIndex = 4;
			this.label3.Text = "Rowname";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtErrorMessage
			// 
			this.txtErrorMessage.Location = new System.Drawing.Point(88, 80);
			this.txtErrorMessage.Name = "txtErrorMessage";
			this.txtErrorMessage.Size = new System.Drawing.Size(320, 20);
			this.txtErrorMessage.TabIndex = 7;
			this.txtErrorMessage.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 80);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 23);
			this.label4.TabIndex = 6;
			this.label4.Text = "Error message";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtLabel
			// 
			this.txtLabel.Location = new System.Drawing.Point(88, 104);
			this.txtLabel.Name = "txtLabel";
			this.txtLabel.Size = new System.Drawing.Size(248, 20);
			this.txtLabel.TabIndex = 9;
			this.txtLabel.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 128);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(56, 23);
			this.label5.TabIndex = 8;
			this.label5.Text = "Tooltip";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtTooltip
			// 
			this.txtTooltip.Location = new System.Drawing.Point(88, 128);
			this.txtTooltip.Name = "txtTooltip";
			this.txtTooltip.Size = new System.Drawing.Size(320, 20);
			this.txtTooltip.TabIndex = 11;
			this.txtTooltip.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 104);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(56, 23);
			this.label6.TabIndex = 10;
			this.label6.Text = "Label";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 152);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(72, 23);
			this.label7.TabIndex = 12;
			this.label7.Text = "Controlled by";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// chkVisible
			// 
			this.chkVisible.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkVisible.Location = new System.Drawing.Point(8, 176);
			this.chkVisible.Name = "chkVisible";
			this.chkVisible.Size = new System.Drawing.Size(96, 24);
			this.chkVisible.TabIndex = 15;
			this.chkVisible.Text = "Visible";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 200);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(56, 23);
			this.label8.TabIndex = 16;
			this.label8.Text = "UI Type";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// chkReload
			// 
			this.chkReload.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkReload.Location = new System.Drawing.Point(8, 224);
			this.chkReload.Name = "chkReload";
			this.chkReload.Size = new System.Drawing.Size(96, 24);
			this.chkReload.TabIndex = 18;
			this.chkReload.Text = "Reload";
			// 
			// chkRecompare
			// 
			this.chkRecompare.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkRecompare.Location = new System.Drawing.Point(8, 248);
			this.chkRecompare.Name = "chkRecompare";
			this.chkRecompare.Size = new System.Drawing.Size(96, 24);
			this.chkRecompare.TabIndex = 19;
			this.chkRecompare.Text = "Recompare";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 304);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(100, 16);
			this.label9.TabIndex = 20;
			this.label9.Text = "Types";
			// 
			// cmbControlledBy
			// 
			this.cmbControlledBy.Location = new System.Drawing.Point(88, 152);
			this.cmbControlledBy.Name = "cmbControlledBy";
			this.cmbControlledBy.Size = new System.Drawing.Size(248, 21);
			this.cmbControlledBy.Sorted = true;
			this.cmbControlledBy.TabIndex = 22;
			// 
			// cmbUIType
			// 
			this.cmbUIType.Location = new System.Drawing.Point(88, 198);
			this.cmbUIType.Name = "cmbUIType";
			this.cmbUIType.Size = new System.Drawing.Size(248, 21);
			this.cmbUIType.TabIndex = 23;
			// 
			// chlTypes
			// 
			this.chlTypes.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.chlTypes.CheckOnClick = true;
			this.chlTypes.Location = new System.Drawing.Point(80, 304);
			this.chlTypes.Name = "chlTypes";
			this.chlTypes.Size = new System.Drawing.Size(272, 184);
			this.chlTypes.Sorted = true;
			this.chlTypes.TabIndex = 25;
			// 
			// cmbUIControlType
			// 
			this.cmbUIControlType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbUIControlType.Items.AddRange(new object[] {
																  "Checkbox",
																  "Textbox"});
			this.cmbUIControlType.Location = new System.Drawing.Point(88, 272);
			this.cmbUIControlType.Name = "cmbUIControlType";
			this.cmbUIControlType.Size = new System.Drawing.Size(248, 21);
			this.cmbUIControlType.Sorted = true;
			this.cmbUIControlType.TabIndex = 27;
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 272);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 23);
			this.label10.TabIndex = 26;
			this.label10.Text = "UI control";
			this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblRowNumber
			// 
			this.lblRowNumber.Location = new System.Drawing.Point(8, 504);
			this.lblRowNumber.Name = "lblRowNumber";
			this.lblRowNumber.Size = new System.Drawing.Size(72, 23);
			this.lblRowNumber.TabIndex = 28;
			this.lblRowNumber.Text = "Row number";
			// 
			// txtRowNumber
			// 
			this.txtRowNumber.Location = new System.Drawing.Point(88, 504);
			this.txtRowNumber.Name = "txtRowNumber";
			this.txtRowNumber.Size = new System.Drawing.Size(208, 20);
			this.txtRowNumber.TabIndex = 29;
			this.txtRowNumber.Text = "";
			// 
			// OptionInfo
			// 
			this.AutoScroll = true;
			this.Controls.Add(this.txtRowNumber);
			this.Controls.Add(this.lblRowNumber);
			this.Controls.Add(this.cmbUIControlType);
			this.Controls.Add(this.label10);
			this.Controls.Add(this.chlTypes);
			this.Controls.Add(this.cmbUIType);
			this.Controls.Add(this.cmbControlledBy);
			this.Controls.Add(this.label9);
			this.Controls.Add(this.chkRecompare);
			this.Controls.Add(this.chkReload);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.chkVisible);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.txtTooltip);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.txtLabel);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.txtErrorMessage);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.txtRowName);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.txtValue);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtKey);
			this.Controls.Add(this.label1);
			this.Name = "OptionInfo";
			this.Size = new System.Drawing.Size(536, 568);
			this.ResumeLayout(false);

		}
		#endregion

		private void UpdateView(StringCollection optionList) 
		{

			string stringValue;
			FillControlledBy(optionList);
			FillUITypes();
			FillUIControlTypes();
			FillTypes();

			if (_configNode != null) 
			{
				txtKey.Text = XmlHelper.GetAttributeValue(_configNode, "Key", string.Empty);
				txtValue.Text = XmlHelper.GetAttributeValue(_configNode, "Value", string.Empty);
				txtRowName.Text = XmlHelper.GetAttributeValue(_configNode, "RowName", string.Empty);
				txtErrorMessage.Text = XmlHelper.GetAttributeValue(_configNode, "ErrorMessage", string.Empty);
				txtLabel.Text = XmlHelper.GetAttributeValue(_configNode, "Label", string.Empty);
				txtTooltip.Text = XmlHelper.GetAttributeValue(_configNode, "ToolTip", string.Empty);
				txtTooltip.Text = XmlHelper.GetAttributeValue(_configNode, "ToolTip", string.Empty);
				cmbControlledBy.Text = XmlHelper.GetAttributeValue(_configNode, "ControlledBy", string.Empty);

				stringValue = XmlHelper.GetAttributeValue(_configNode, "Visible", string.Empty);
				chkVisible.Checked = (stringValue.Trim().ToLower() == "true");
				
				cmbUIType.Text = XmlHelper.GetAttributeValue(_configNode, "UIType", string.Empty);
				cmbUIControlType.Text = XmlHelper.GetAttributeValue(_configNode, "CompareOptionControlType", string.Empty);

				stringValue = XmlHelper.GetAttributeValue(_configNode, "Reload", string.Empty);
				chkReload.Checked = (stringValue.Trim().ToLower() == "true");

				stringValue = XmlHelper.GetAttributeValue(_configNode, "Recompare", string.Empty);
				chkRecompare.Checked = (stringValue.Trim().ToLower() == "true");

				txtRowNumber.Text = XmlHelper.GetAttributeValue(_configNode, "RowNumber",true);

				XmlNode typesNode = _configNode.SelectSingleNode("Types");
				if (typesNode != null) 
				{
					XmlNodeList typeNodes = typesNode.SelectNodes("Type");
					string typeName;
					if (typeNodes != null) 
					{
						foreach (XmlNode typeNode in typeNodes) 
						{
							typeName = XmlHelper.GetAttributeValue(typeNode, "Name", string.Empty);

							if (chlTypes.Items.Contains(typeName)) 
							{
								chlTypes.SetItemChecked(chlTypes.Items.IndexOf(typeName), true);
							}
							else
							{
								chlTypes.Items.Add(typeName, true);
							}
						}
					}
				}
			}
		}

		public XmlNode GetData() 
		{
			string tableName = string.Empty;
			if (_configNode == null) 
				throw new ApplicationException("ERROR: Option data is not initialized!");

			_configNode.Attributes["Key"].Value = txtKey.Text; 
			_configNode.Attributes["Value"].Value = txtValue.Text; 
			_configNode.Attributes["RowName"].Value = txtRowName.Text; 
			_configNode.Attributes["ErrorMessage"].Value = txtErrorMessage.Text; 
			_configNode.Attributes["Label"].Value = txtLabel.Text; 
			_configNode.Attributes["ToolTip"].Value = txtTooltip.Text; 
			_configNode.Attributes["ControlledBy"].Value = cmbControlledBy.Text; 
			_configNode.Attributes["Visible"].Value = chkVisible.Checked.ToString();
			_configNode.Attributes["UIType"].Value = cmbUIType.Text; 
			_configNode.Attributes["CompareOptionControlType"].Value = cmbUIControlType.Text; 
			_configNode.Attributes["Reload"].Value = chkReload.Checked.ToString();
			_configNode.Attributes["Recompare"].Value = chkRecompare.Checked.ToString();
			_configNode.Attributes["RowNumber"].Value = txtRowNumber.Text;

			foreach (string type in chlTypes.CheckedItems) 
			{
				if (tableName.Length > 0)
					tableName += "@";
				tableName += type;
			}
			_configNode.Attributes["TableName"].Value = tableName;

			XmlNode typesNode = _configNode.SelectSingleNode("Types");
			if (typesNode != null) 
			{
				typesNode.RemoveAll();

				foreach (string type in chlTypes.CheckedItems) 
				{
					XmlHelper.CreateXmlNode(typesNode.OwnerDocument, typesNode, "Type", 
						new string[] {"Name"},
						new string[] {type}
						);
				}
			}
			return _configNode;
		}
		private void FillControlledBy(StringCollection optionList) 
		{
			cmbControlledBy.Items.Clear();
			cmbControlledBy.Text = string.Empty;
			if (optionList != null) 
			{
				foreach (string option in optionList) 
					cmbControlledBy.Items.Add(option);
			}
		}
		private void FillUITypes() 
		{
			cmbUIType.Items.Clear();
			for (int i=0; i<DbDiffUtilsConfig.Instance.UITypes.Length; i++) 
			{
				cmbUIType.Items.Add(DbDiffUtilsConfig.Instance.UITypes[i]);
			}
		}

		private void FillUIControlTypes() 
		{
			cmbUIControlType.Items.Clear();
			cmbUIControlType.Text = string.Empty;
			cmbUIControlType.Items.Add("checkbox");
			cmbUIControlType.Items.Add("textbox");
		}
		private void FillTypes() 
		{
			chlTypes.Items.Clear();
			string[] types = Enum.GetNames(typeof(DatabaseObjectTypes));
			foreach (string type in types) 
			{
				chlTypes.Items.Add(type, CheckState.Unchecked);
			}
		}
	}
}

